package com.xinchuang.service.targetSystem.Impl.indexUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xinchuang.comment.R;
import com.xinchuang.entity.IndexCompute.*;
import com.xinchuang.entity.targetSystem.target.SystemIndex;
import com.xinchuang.mapper.targetSystem.target.SystemIndexMapper;
import com.xinchuang.service.RedisUseService;
import com.xinchuang.service.targetSystem.indexUtil.IndexComputeService;
import com.xinchuang.service.targetSystem.indexUtil.IndexValueService;
import com.xinchuang.util.RedisUtil;
import com.xinchuang.utils.RedisName;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * TODO
 *
 * @author zkwczx
 * @version 1.0
 * @date 2024/4/16 14:45
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class IndexComputeServiceImpl implements IndexComputeService {

    private final SystemIndexMapper systemIndexMapper;

    private final RedisUseService redisUseService;

    private final IndexValueService indexValueService;

    private final RedisUtil redisUtil;

    private static String IS_ADD = "isAdd";

    @Override
    public boolean enterCompute(SystemIndex index, JSONObject json) {
        log.info("传入数据：" + json.toJSONString());
        //获取指标类型
        String indexType = json.getString("pullValue");
        if (StringUtils.isEmpty(indexType)) {
            indexType = "";
        }
        switch (indexType) {
            case "oneFieldRange":
                return oneFieldRangeAdd(index, json);
            case "targetQuantity":
                return targetQuantityAdd(index, json);
            case "targetType":
                return targetTypeAdd(index, json);
            case "targetPosition":
                return targetPositionAdd(index, json);
            case "targetDistance":
                return targetDistanceAdd(index, json);
            case "targetPositionDistance":
                return targetPositionDistanceAdd(index, json);
            case "divide":
                return divideAdd(index, json);
            case "paramesScope":
                return paramesScopeAdd(index, json);
            case "numberPoints":
                return numberPointsAdd(index, json);
            case "numberPointsNo":
                return numberPointsNoAdd(index, json);
            case "haveScore":
                return haveScoreAdd(index, json);
            case "resultStatistics":
                return resultStatisticsAdd(index, json);
            case "fixedScore":
                return fixedScoreAdd(index, json);
            case "custom":
                return customAdd(index, json);
            case "selectValue":
                return selectValueAdd(index, json);
            default:
                index.setIndicatorValue("0");
                return false;
        }
    }

    @Override
    public R enterComputeWay(SystemIndex index, JSONObject json) {
        log.info("传入数据：" + json.toJSONString());
        //获取指标类型
        String indexType = json.getString("pullValue");
        if (StringUtils.isEmpty(indexType)) {
            indexType = "";
        }
        if (StringUtils.isNotEmpty(index.getMsgType())) {
            indexType = index.getMsgType();
        }
        switch (indexType) {
            case "oneFieldRange":
                return oneFieldRange(index, json);
            case "targetQuantity":
                return targetQuantity(index, json);
            case "targetType":
                return targetType(index, json);
            case "targetPosition":
                return targetPosition(index, json);
            case "targetDistance":
                return targetDistance(index, json);
            case "targetPositionDistance":
                return targetPositionDistance(index, json);
            case "divide":
                return divide(index, json);
            case "paramesScope":
                return paramesScope(index, json);
            case "numberPoints":
                return numberPoints(index, json);
            case "numberPointsNo":
                return numberPointsNo(index, json);
            case "haveScore":
                return haveScore(index, json);
            case "resultStatistics":
                return resultStatistics(index, json);
            case "fixedScore":
                return fixedScore(index, json);
            case "custom":
                return custom(index, json);
            case "selectValue":
                return selectValue(index, json);
            default:
                return R.data(resultData(index, "", ""));
        }
    }

    private JSONObject resultData(SystemIndex index, String pullValue, Object obj) {
        JSONObject result = new JSONObject();
        result.put("indicatorId", index.getId());
        result.put("redId", index.getRedId());
        result.put("parentId", index.getParentId());
        result.put("indicatorName", index.getIndicatorName());
        result.put("indicatorValue", index.getIndicatorValue());
        result.put("nodeType", index.getType());
        result.put("remark", index.getRemark());
        result.put("pullValue", pullValue);
        result.put("indexValue", obj);
        log.info("返回参数:" + result.toJSONString());
        return result;
    }

    public boolean oneFieldRangeAdd(SystemIndex index, JSONObject json) {
        //判断更新还是新增
        OneFieldRange oneFieldRange = json.getObject("indexValue", OneFieldRange.class);
        index.setIndicatorValue("0");
        index.setMsgType("oneFieldRange");
        index.setIndexMsg(JSON.toJSONString(oneFieldRange));
        //目标实际计算
        if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + index.getRedId())) {
            double value = indexValueService.entryIndexValue(index.getMsgType(), index.getIndexMsg(), redisUseService.isTarget(index.getRedId()), index.getRedId());
            index.setIndicatorValue(value + "");
            return true;
        }
        return false;
    }

    public R oneFieldRange(SystemIndex index, JSONObject json) {
        if (StringUtils.isEmpty(index.getMsgType())) {
            return R.data(resultData(index, "oneFieldRange", new OneFieldRange()));
        } else {
            return R.data(resultData(index, "oneFieldRange", JSONObject.parseObject(index.getIndexMsg())));
        }
    }

    public boolean targetQuantityAdd(SystemIndex index, JSONObject json) {
        TargetQuantity targetQuantity = json.getObject("indexValue", TargetQuantity.class);
        index.setIndicatorValue("0");
        index.setMsgType("targetQuantity");
        index.setIndexMsg(JSON.toJSONString(targetQuantity));
        //目标实际计算
        if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + index.getRedId())) {
            double value = indexValueService.entryIndexValue(index.getMsgType(), index.getIndexMsg(), redisUseService.isTarget(index.getRedId()), index.getRedId());
            index.setIndicatorValue(value + "");
            return true;
        }
        return false;
    }

    public R targetQuantity(SystemIndex index, JSONObject json) {
        if (StringUtils.isEmpty(index.getMsgType())) {
            return R.data(resultData(index, "targetQuantity", new TargetQuantity()));
        } else {
            return R.data(resultData(index, "targetQuantity", JSONObject.parseObject(index.getIndexMsg())));
        }
    }

    public boolean targetTypeAdd(SystemIndex index, JSONObject json) {
        TargetTypeIndex targetType = json.getObject("indexValue", TargetTypeIndex.class);
        index.setIndicatorValue("0");
        index.setMsgType("targetType");
        index.setIndexMsg(JSON.toJSONString(targetType));
        //目标实际计算
        if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + index.getRedId())) {
            double value = indexValueService.entryIndexValue(index.getMsgType(), index.getIndexMsg(), redisUseService.isTarget(index.getRedId()), index.getRedId());
            index.setIndicatorValue(value + "");
            return true;
        }
        return false;
    }

    public R targetType(SystemIndex index, JSONObject json) {
        if (StringUtils.isEmpty(index.getMsgType())) {
            return R.data(resultData(index, "targetType", new TargetTypeIndex()));
        } else {
            return R.data(resultData(index, "targetType", JSONObject.parseObject(index.getIndexMsg())));
        }
    }

    public boolean targetPositionAdd(SystemIndex index, JSONObject json) {
        TargetPosition targetType = json.getObject("indexValue", TargetPosition.class);
        index.setIndicatorValue("0");
        index.setMsgType("targetPosition");
        index.setIndexMsg(JSON.toJSONString(targetType));
        //目标实际计算
        if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + index.getRedId())) {
            double value = indexValueService.entryIndexValue(index.getMsgType(), index.getIndexMsg(), redisUseService.isTarget(index.getRedId()), index.getRedId());
            index.setIndicatorValue(value + "");
            return true;
        }
        return false;
    }

    public R targetPosition(SystemIndex index, JSONObject json) {
        if (StringUtils.isEmpty(index.getMsgType())) {
            return R.data(resultData(index, "targetPosition", new TargetPosition()));
        } else {
            return R.data(resultData(index, "targetPosition", JSONObject.parseObject(index.getIndexMsg())));
        }
    }

    public boolean targetDistanceAdd(SystemIndex index, JSONObject json) {
        TargetDistance targetType = json.getObject("indexValue", TargetDistance.class);
        index.setIndicatorValue("0");
        index.setMsgType("targetDistance");
        index.setIndexMsg(JSON.toJSONString(targetType));
        //目标实际计算
        if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + index.getRedId())) {
            double value = indexValueService.entryIndexValue(index.getMsgType(), index.getIndexMsg(), redisUseService.isTarget(index.getRedId()), index.getRedId());
            index.setIndicatorValue(value + "");
            return true;
        }
        return false;
    }

    public R targetDistance(SystemIndex index, JSONObject json) {
        if (StringUtils.isEmpty(index.getMsgType())) {
            return R.data(resultData(index, "targetDistance", new TargetDistance()));
        } else {
            return R.data(resultData(index, "targetDistance", JSONObject.parseObject(index.getIndexMsg())));
        }
    }

    public boolean targetPositionDistanceAdd(SystemIndex index, JSONObject json) {
        TargetPositionDistance targetType = json.getObject("indexValue", TargetPositionDistance.class);
        index.setIndicatorValue("0");
        index.setMsgType("targetPositionDistance");
        index.setIndexMsg(JSON.toJSONString(targetType));
        //目标实际计算
        if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + index.getRedId())) {
            double value = indexValueService.entryIndexValue(index.getMsgType(), index.getIndexMsg(), redisUseService.isTarget(index.getRedId()), index.getRedId());
            index.setIndicatorValue(value + "");
            return true;
        }
        return false;
    }

    public R targetPositionDistance(SystemIndex index, JSONObject json) {
        if (StringUtils.isEmpty(index.getMsgType())) {
            return R.data(resultData(index, "targetPositionDistance", new TargetPositionDistance()));
        } else {
            return R.data(resultData(index, "targetPositionDistance", JSONObject.parseObject(index.getIndexMsg())));
        }
    }

    public  boolean divideAdd(SystemIndex index, JSONObject json) {
        Divide divide = json.getObject("indexValue", Divide.class);
        index.setIndicatorValue("0");
        index.setMsgType("divide");
        index.setIndexMsg(JSON.toJSONString(divide));
        //目标实际计算
        if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + index.getRedId())) {
            double value = indexValueService.entryIndexValue(index.getMsgType(), index.getIndexMsg(), redisUseService.isTarget(index.getRedId()), index.getRedId());
            index.setIndicatorValue(value + "");
            return true;
        }
        return false;
    }

    public R divide(SystemIndex index, JSONObject json) {
        if (StringUtils.isEmpty(index.getMsgType())) {
            return R.data(resultData(index, "divide", new Divide()));
        } else {
            return R.data(resultData(index, "divide", JSONObject.parseObject(index.getIndexMsg())));
        }
    }

    public boolean paramesScopeAdd(SystemIndex index, JSONObject json) {
        ParamesScope targetType = json.getObject("indexValue", ParamesScope.class);
        index.setIndicatorValue("0");
        index.setMsgType("paramesScope");
        index.setIndexMsg(JSON.toJSONString(targetType));
        //目标实际计算
        if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + index.getRedId())) {
            double value = indexValueService.entryIndexValue(index.getMsgType(), index.getIndexMsg(), redisUseService.isTarget(index.getRedId()), index.getRedId());
            index.setIndicatorValue(value + "");
            return true;
        }
        return false;
    }

    public R paramesScope(SystemIndex index, JSONObject json) {
        if (StringUtils.isEmpty(index.getMsgType())) {
            return R.data(resultData(index, "paramesScope", new ParamesScope()));
        } else {
            return R.data(resultData(index, "paramesScope", JSONObject.parseObject(index.getIndexMsg())));
        }
    }

    public boolean numberPointsAdd(SystemIndex index, JSONObject json) {
        NumberPoints targetType = json.getObject("indexValue", NumberPoints.class);
        index.setIndicatorValue("0");
        index.setMsgType("numberPoints");
        index.setIndexMsg(JSON.toJSONString(targetType));
        //目标实际计算
        if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + index.getRedId())) {
            double value = indexValueService.entryIndexValue(index.getMsgType(), index.getIndexMsg(), redisUseService.isTarget(index.getRedId()), index.getRedId());
            index.setIndicatorValue(value + "");
        }
        return false;
    }

    public R numberPoints(SystemIndex index, JSONObject json) {
        if (StringUtils.isEmpty(index.getMsgType())) {
            return R.data(resultData(index, "numberPoints", new NumberPoints()));
        } else {
            return R.data(resultData(index, "numberPoints", JSONObject.parseObject(index.getIndexMsg())));
        }
    }

    public boolean numberPointsNoAdd(SystemIndex index, JSONObject json) {
        NumberPointsNo targetType = json.getObject("indexValue", NumberPointsNo.class);
        index.setIndicatorValue("0");
        index.setMsgType("numberPointsNo");
        index.setIndexMsg(JSON.toJSONString(targetType));
        //目标实际计算
        if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + index.getRedId())) {
            double value = indexValueService.entryIndexValue(index.getMsgType(), index.getIndexMsg(), redisUseService.isTarget(index.getRedId()), index.getRedId());
            index.setIndicatorValue(value + "");
            return true;
        }
        return false;
    }

    public R numberPointsNo(SystemIndex index, JSONObject json) {
        if (StringUtils.isEmpty(index.getMsgType())) {
            return R.data(resultData(index, "numberPointsNo", new NumberPointsNo()));
        } else {
            return R.data(resultData(index, "numberPointsNo", JSONObject.parseObject(index.getIndexMsg())));
        }
    }

    public boolean haveScoreAdd(SystemIndex index, JSONObject json) {
        HaveScore targetType = json.getObject("indexValue", HaveScore.class);
        index.setIndicatorValue("0");
        index.setMsgType("haveScore");
        index.setIndexMsg(JSON.toJSONString(targetType));
        //目标实际计算
        if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + index.getRedId())) {
            double value = indexValueService.entryIndexValue(index.getMsgType(), index.getIndexMsg(), redisUseService.isTarget(index.getRedId()), index.getRedId());
            index.setIndicatorValue(value + "");
            return true;
        }
        return false;
    }

    public R haveScore(SystemIndex index, JSONObject json) {
        if (StringUtils.isEmpty(index.getMsgType())) {
            return R.data(resultData(index, "haveScore", new HaveScore()));
        } else {
            return R.data(resultData(index, "haveScore", JSONObject.parseObject(index.getIndexMsg())));
        }
    }

    public boolean resultStatisticsAdd(SystemIndex index, JSONObject json) {
        ResultStatistics targetType = json.getObject("indexValue", ResultStatistics.class);
        index.setIndicatorValue("0");
        index.setMsgType("resultStatistics");
        index.setIndexMsg(JSON.toJSONString(targetType));
        //目标实际计算
        if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + index.getRedId())) {
            double value = indexValueService.entryIndexValue(index.getMsgType(), index.getIndexMsg(), redisUseService.isTarget(index.getRedId()), index.getRedId());
            index.setIndicatorValue(value + "");
            return true;
        }
        return false;
    }

    public R resultStatistics(SystemIndex index, JSONObject json) {
        if (StringUtils.isEmpty(index.getMsgType())) {
            return R.data(resultData(index, "resultStatistics", new ResultStatistics()));
        } else {
            return R.data(resultData(index, "resultStatistics", JSONObject.parseObject(index.getIndexMsg())));
        }
    }

    public boolean fixedScoreAdd(SystemIndex index, JSONObject json) {
        FixedScore targetType = json.getObject("indexValue", FixedScore.class);
        index.setIndicatorValue("0");
        index.setMsgType("fixedScore");
        index.setIndexMsg(JSON.toJSONString(targetType));
        //目标实际计算
        if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + index.getRedId())) {
            double value = indexValueService.entryIndexValue(index.getMsgType(), index.getIndexMsg(), redisUseService.isTarget(index.getRedId()), index.getRedId());
            index.setIndicatorValue(value + "");
            return true;
        }
        return false;
    }

    public R fixedScore(SystemIndex index, JSONObject json) {
        if (StringUtils.isEmpty(index.getMsgType())) {
            return R.data(resultData(index, "fixedScore", new FixedScore()));
        } else {
            return R.data(resultData(index, "fixedScore", JSONObject.parseObject(index.getIndexMsg())));
        }
    }

    public boolean customAdd(SystemIndex index, JSONObject json) {
        Custom targetType = json.getObject("indexValue", Custom.class);
        index.setIndicatorValue("0");
        index.setMsgType("custom");
        index.setIndexMsg(JSON.toJSONString(targetType));
        //目标实际计算
        if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + index.getRedId())) {
            double value = indexValueService.entryIndexValue(index.getMsgType(), index.getIndexMsg(), redisUseService.isTarget(index.getRedId()), index.getRedId());
            index.setIndicatorValue(value + "");
            return true;
        }
        return false;
    }

    public R custom(SystemIndex index, JSONObject json) {
        if (StringUtils.isEmpty(index.getMsgType())) {
            return R.data(resultData(index, "custom", new Custom()));
        } else {
            return R.data(resultData(index, "custom", JSONObject.parseObject(index.getIndexMsg())));
        }
    }

    public boolean selectValueAdd(SystemIndex index, JSONObject json) {
        SelectValue targetType = json.getObject("indexValue", SelectValue.class);
        index.setIndicatorValue("0");
        index.setMsgType("selectValue");
        index.setIndexMsg(JSON.toJSONString(targetType));
        //目标实际计算
        if (redisUtil.hasKey(RedisName.TARGET_BASE_DATA_ALL + "_" + index.getRedId())) {
            double value = indexValueService.entryIndexValue(index.getMsgType(), index.getIndexMsg(), redisUseService.isTarget(index.getRedId()), index.getRedId());
            index.setIndicatorValue(value + "");
            return true;
        }
        return false;
    }

    public R selectValue(SystemIndex index, JSONObject json) {
        if (StringUtils.isEmpty(index.getMsgType())) {
            return R.data(resultData(index, "selectValue", new SelectValue()));
        } else {
            return R.data(resultData(index, "selectValue", JSONObject.parseObject(index.getIndexMsg())));
        }
    }

}
